/*
 *  app_led.h -- provide ISSI IS31FL3216 led driver chip operation interface.
 *
 *  ORIGINAL AUTHOR: Xu Chun (chun.xu@milliwave.cn)
 */

#ifndef __APP_LED_H_
#define __APP_LED_H_

#include "mc200_gpio.h"
#include "mc200_pmu.h"
#include "mc200_pinmux.h"

/* Microcontroller related pins (the chip using I2C interface) */
#define CHIP_I2C_SDA       GPIO_22
#define CHIP_I2C_SCL       GPIO_19
#define CHIP_POWER_EN      GPIO_28

/* I2C operation interface */
#define CHIP_I2C_BASE_ADDR 0xE8           /* 11101 + AD + R/W */
#define CHIP_AD_SEL0       (0x00 << 1)    /* AD connected to GND */
#define CHIP_AD_SEL1       (0x01 << 1)    /* AD connected to SCL */
#define CHIP_AD_SEL2       (0x10 << 1)    /* AD connected to SDA */
#define CHIP_AD_SEL3       (0x11 << 1)    /* AD conneted to VCC */
#define CHIP_WRITE         0x00
#define CHIP_READ          0x01

/* IIC Low-level operation sets */ 
#define scl_low()           GPIO_WritePinOutput(CHIP_I2C_SCL, GPIO_IO_LOW)
#define scl_high()          GPIO_WritePinOutput(CHIP_I2C_SCL, GPIO_IO_HIGH)
#define sda_low()           GPIO_WritePinOutput(CHIP_I2C_SDA, GPIO_IO_LOW)
#define sda_high()          GPIO_WritePinOutput(CHIP_I2C_SDA, GPIO_IO_HIGH) 
#define set_scl_output()    GPIO_SetPinDir(CHIP_I2C_SCL, GPIO_OUTPUT)
#define set_sda_output()    GPIO_SetPinDir(CHIP_I2C_SDA, GPIO_OUTPUT)
#define set_sda_input()     GPIO_SetPinDir(CHIP_I2C_SDA, GPIO_INPUT)
#define enable_iic_power()  PMU_PowerOnVDDIO(PMU_VDDIO_D1);
#define enable_scl_pin()    GPIO_PinMuxFun(CHIP_I2C_SCL, PINMUX_FUNCTION_0);
#define enable_sda_pin()    GPIO_PinMuxFun(CHIP_I2C_SDA, PINMUX_FUNCTION_0);
#define enable_scl_pullup() GPIO_PinModeConfig(CHIP_I2C_SCL, PINMODE_PULLUP);
#define enable_sda_pullup() GPIO_PinModeConfig(CHIP_I2C_SDA, PINMODE_PULLUP);
#define get_sda_state()     GPIO_ReadPinLevel(CHIP_I2C_SDA)

/* IIC ACK define */
typedef enum {
	ACK    = 0,
	NO_ACK = 1,
}I2cAck;

/* Chip register list */
#define CHIP_CONFIG         0x00
#define CHIP_LED0_CONTROL   0x01           /* OUT9 - OUT16 enable bit */ 
#define CHIP_LED1_CONTROL   0x02           /* OUT1 - OUT8 enable bit */
#define CHIP_OUTPUT_CURRENT 0x03
#define CHIP_CHANNEL_CONFIG 0x04
#define CHIP_PWM_CONFIG     0x10           /* Set the PWM duty cycle data: 10h - 1Fh */
#define CHIP_PWM_UPDATE     0xB0

typedef enum led_current_e {
    ILEDX1_0,                              /* ILED X 1.0 */
    ILEDX0_75,                             /* ILED X 0.75 */
    ILEDX0_5,                              /* ILED X 0.5 */
    ILEDX0_25,                             /* ILED X 0.25 */
    ILEDX2_0,                              /* ILED X 2.0 */
    ILEDX1_75,                             /* ILED X 1.75 */
    ILEDX1_5,                              /* ILED X 1.5 */
    ILEDX1_25                              /* ILED X 1.25 */
}led_current_e;

typedef enum led_channel_mode_e {
    LEDOUTPUT,
    GPIOPORT
}led_channel_mode_e;

typedef enum led_channel_e {
    OUTPUT16 = 0x10,
    OUTPUT15,
    OUTPUT14,
    OUTPUT13,
    OUTPUT12,
    OUTPUT11,
    OUTPUT10,
    OUTPUT9,
    OUTPUT8,
    OUTPUT7,
    OUTPUT6,
    OUTPUT5,
    OUTPUT4,
    OUTPUT3,
    OUTPUT2,
    OUTPUT1
}led_channel_e;

int led_chip_current_config(led_current_e current);
int led_chip_channel_config(led_channel_mode_e channel);
int led_chip_pwm_config(led_channel_e channel, uint8_t intensity, uint8_t count);
int led_chip_init(void);
int led_chip_test1(void);
int led_chip_test2(void);

#endif
